home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6231 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  48 lines

  1. Newsgroups: comp.lang.c
  2. Path: mxsld2.pd.infn.it!LORETI
  3. From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
  4. Subject: Re: What to do when feof() is NOT feof()
  5. X-Nntp-Posting-Host: mxsld2.pd.infn.it
  6. Message-ID: <Dn7x8s.53s@news.cern.ch>
  7. Sender: news@news.cern.ch (USENET News System)
  8. Reply-To: loreti@mxsld2.pd.infn.it
  9. Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
  10. References: <824554814snz@metsys.demon.co.uk>
  11. Date: Fri, 23 Feb 1996 07:38:50 GMT
  12.  
  13. In article <824554814snz@metsys.demon.co.uk>, John Standen <John@metsys.demon.co.uk> writes:
  14. >I have a function in a program which is reading each character
  15. >in a file of some 700k characters.
  16. >
  17. >The do{ while !feof(in) sometimes causes the function to terminate
  18. >very early in the file.
  19.  
  20. Hmmm, you did not notice, reading the FAQ list before posting, Q/A
  21. 12.2:
  22.  
  23. 12.2:    Why does the code
  24.  
  25.         while(!feof(infp)) {
  26.             fgets(buf, MAXLINE, infp);
  27.             fputs(buf, outfp);
  28.         }
  29.  
  30.     copy the last line twice?
  31.  
  32. A:    In C, EOF is only indicated *after* an input routine has tried
  33.     to read, and has reached end-of-file.  (In other words, C's I/O
  34.     is not like Pascal's.)  Usually, you should just check the
  35.     return value of the input routine (fgets() in this case); often,
  36.     you don't need to use feof() at all.
  37.  
  38.     References: K&R2 Sec. 7.6 p. 164; ANSI Sec. 4.9.3, Sec. 4.9.7.1,
  39.     Sec. 4.9.10.2; ISO Sec. 7.9.3, Sec. 7.9.7.1, Sec. 7.9.10.2; H&S
  40.     Sec. 15.14 p. 382.
  41.  
  42. 12.2 states that your approach to the file is wrong; however the error
  43. should appear at the end of the file, not in the middle.  Are you sure
  44. your file does not contain any control characters?
  45. --
  46. Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
  47. Un. of Padova, Dept. of Physics - Padova, Italy          loreti@padova.infn.it
  48.